<template>
  <div class="content">
    <div class="container-box-left container-box">
      <div class="list-content">
        <el-tree
            ref="deptTreeDataRef"
            default-expand-all
            :data="deptTreeData"
            :props="defaultProps"
            highlight-current
            @node-click="handleNodeClick"
            :filter-node-method="filterNode"
            check-strictly
            :check-on-click-node="false"
            :expand-on-click-node="false"
            :check-on-click-leaf="false"
        >
          <template #default="{ node }">
            <span class="custom-tree-node" :title="node.label">
              <span>{{ node.label }}</span>
            </span>
          </template></el-tree
        >
      </div>
    </div>
    <div class="container-box-right">
      <div class="container-box">
        <div class="form-box form-search-box">
          <search-box>
            <template #form>
              <el-form :inline="true" class="form-wrap">
                <el-form-item label="工号：">
                  <el-input
                      v-model="formInline.workNumber"
                      placeholder="请输入"
                      clearable
                  />
                </el-form-item>
                <el-form-item label="姓名：">
                  <el-input
                      v-model="formInline.name"
                      placeholder="请输入"
                      clearable
                  />
                </el-form-item>
                <el-form-item label="准驾车型：">
                  <el-select
                      v-model="formInline.carType"
                      placeholder="请输入"
                      clearable
                  >
                    <el-option
                        :key="index"
                        v-for="(item, index) in carTypeList"
                        :label="item.label"
                        :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-form>
            </template>
            <template #btn>
              <el-button
                  type="primary"
                  :loading="searchLoading"
                  @click="getList(1)"
              >查询</el-button
              >
              <el-button type="primary" plain @click="onSubmit">重置</el-button>
            </template>
          </search-box>
          <div class="form-add2">
            <el-button type="primary" @click="openAdd()">新增</el-button>
          </div>
        </div>
        <div class="table-box">
          <el-table :data="tableData" stripe v-loading="searchLoading">
            <el-table-column
                type="index"
                align="center"
                label="序号"
                width="80"
            />
            <el-table-column
                prop="user['name']"
                label="姓名"
                min-width="120"
                align="center"
                show-overflow-tooltip
            />
            <el-table-column
                prop="user['workNumber']"
                label="工号"
                min-width="100"
                align="center"
                show-overflow-tooltip
            />
            <el-table-column
                prop="user['userName']"
                label="用户名"
                min-width="120"
                align="center"
                show-overflow-tooltip
            />
            <el-table-column
                prop="user['cardNumber']"
                min-width="120"
                label="定位卡号"
                align="center"
                show-overflow-tooltip
            />
            <el-table-column
                prop="user['departmentName']"
                width="200"
                label="所属部门"
                align="center"
                show-overflow-tooltip
            />
            <el-table-column
                prop="user['roleName']"
                width="140"
                label="角色"
                align="center"
                show-overflow-tooltip
            />
            <el-table-column
                prop="carType"
                width="120"
                label="准驾车型"
                align="center"
                show-overflow-tooltip
            />
            <el-table-column
                prop="state"
                width="120"
                label="状态"
                align="center"
                show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
                width="180"
                label="操作"
                align="center"
                fixed="right"
            >
              <template v-slot="{ row }">
                <el-button link type="primary" @click="openAdd(row)"
                >编辑</el-button
                >
                <el-button link type="danger" @click="deleteHandle(row)"
                >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>

        <div class="pagination-box">
          <el-pagination
              v-model:current-page="formInline.pageIndex"
              v-model:page-size="formInline.pageSize"
              :page-sizes="[20, 30, 40, 50]"
              layout="total, prev, pager, next,sizes, jumper"
              :total="total"
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import treeByDept from "@/views/userManagement/treeByDept";
const formInline = ref({
  workNumber: "",
  name: "",
  carType: "",
  departmentId: "",
  // date: "",
  pageIndex: 1,
  pageSize: 20,
});
const searchFormNumber = ref("");
const total = ref(400);
const carTypeList = ref([]);
const tableData = ref([]);
const searchLoading = ref(false);
watch(searchFormNumber, (val) => {
  console.log(val, ".===--");
  deptTreeDataRef.value.filter(val);
});
const defaultProps = {
  children: "children",
  label: "name",
};
const deptTreeDataRef = ref();
const deptTreeData = ref([]);

const handleNodeClick = (data) => {
  formInline.value.departmentId = data.id;
  getList();
};
const filterNode = (value, data) => {
  if (!value) return true;
  return data.name.includes(value);
};

const  openAdd = (row)=>{
  let url = "";
  let title = "";
  if (row) {
    url = "organizationalStructureAddOrEdit?id=" + row.id;
    title = "编辑";
  } else {
    url = "organizationalStructureAddOrEdit";
    title = "新增";
  }
  openDialog(
      {
        path: window.rootUrl + url,
        width: 1000,
        height:520,
        title: title,
        close: (res) => {
          console.log(res);
        },
      },
      (res) => {
        res && getList();
      }
  );
}
const deleteHandle = (row)=>{
  ElMessageBox.confirm('确定删除该部门？','提示',{
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  }).then(()=>{
    console.log(111)
  }).catch(()=>{})
}

onMounted(() => {
  deptTreeData.value = treeByDept
  getList();
});
const getList = async (page) => {
  searchLoading.value = true;
  formInline.value.pageIndex = page ? page : formInline.value.pageIndex;

  tableData.value = [{}]
  searchLoading.value = false
};

const onSubmit = () => {
  console.log("submit!");
  formInline.value = {
    workNumber: "",
    name: "",
    carType: "",
    departmentId: "",
    // date: "",
    pageIndex: 1,
    pageSize: 20,
  };
  getList();
};
const handleSizeChange = (val) => {
  getList();
};
const handleCurrentChange = (val) => {
  getList();
};
</script>
<style lang="scss" scoped>
.content {
  display: flex;
  height: 100%;

  .container-box-left {
    width: 279px;
    padding: 10px 18px;
    // background-color: #ffffff;
    border-radius: 8px;

    .list-content {
      margin-top: 10px;
      overflow: auto;
    }
  }
  .container-box-right {
    margin-left: 10px;
    width: 0;
    flex: 1;
  }
}

.title {
  line-height: 40px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .title-info {
    font-size: 16px;
    display: flex;
    align-items: center;
    img {
      margin-right: 10px;
    }
  }
  .title-tips {
    font-size: 12px;
    color: #fe5e5e;
  }
}
.custom-tree-node {
  width: 0;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
